﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using MG_WIN.Util;

namespace MG_WIN.DAL
{
    /// <summary>
    /// 数据访问类:ItemSetting
    /// </summary>
    public partial class ItemSetting
    {
        public ItemSetting()
        { }
        #region  Method


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string F01)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_item_setting");
            strSql.Append(" where F01='" + F01 + "' ");
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(MG_WIN.Model.ItemSetting model)
        {
            DbHelper db = new DbHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_item_setting(");
            strSql.Append("F01,F02,F03,F04,F08,F19,F23,F24,F27,F29,F30,F31,F38,F79,F81,F82,F178,F924,create_datetime,update_datetime,F106,F96,F97,F98,F150,F158,is_loyalty,is_tare,is_age_ver,F99,F100,is_coupon,service_fl,printer_id,is_mult_coupon,is_price_lvl");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + model.upc + "',");
            strSql.Append("'" + model.name + "',");
            strSql.Append("" + model.dep + ",");
            strSql.Append("" + model.subDep + ",");
            strSql.Append("'" + model.active + "',");
            strSql.Append("" + model.casePack + ",");
            strSql.Append("'" + model.netWT + "',");
            strSql.Append("" + model.wt + ",");
            strSql.Append("'" + model.vendor + "',");
            strSql.Append("'" + model.desc + "',");
            strSql.Append("" + model.price + ",");
            strSql.Append("" + model.multple + ",");
            strSql.Append("" + model.caseCost + ",");
            strSql.Append("'" + model.foodStamp + "',");
            strSql.Append("'" + model.tax1 + "',");
            strSql.Append("'" + model.scaleWeight + "',");
            strSql.Append("'" + model.wic + "',");
            strSql.Append("'" + model.cvv + "',");
            strSql.Append("'" + model.create_datetime + "',");
            strSql.Append("'" + model.update_datetime + "',");
            strSql.Append("'" + model.followdep + "',");
            strSql.Append("'" + model.tax2 + "',");
            strSql.Append("'" + model.tax3 + "',");
            strSql.Append("'" + model.tax4 + "',");
            strSql.Append("'" + model.item_discount_flag + "',");
            strSql.Append("'" + model.bottle_deposit + "',");
            strSql.Append("'" + model.is_loyalty + "',");
            strSql.Append("" + model.is_tare + ",");
            strSql.Append("" + model.is_age_ver + ",");
            strSql.Append("'" + model.tax5 + "',");
            strSql.Append("'" + model.tax6 + "',");
            strSql.Append("'" + model.is_coupon + "',");
            strSql.Append("'" + model.service_fl + "',");
            strSql.Append("" + model.printer_id + ",");
            strSql.Append("'" + model.is_mult_coupon + "',");
            strSql.Append("" + model.is_price_lvl + "");
            strSql.Append(")");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(MG_WIN.Model.ItemSetting model)
        {
            DbHelper db = new DbHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_item_setting set ");
            strSql.Append("F02='" + model.name + "',");
            strSql.Append("F03=" + model.dep + ",");
            strSql.Append("F04=" + model.subDep + ",");
            strSql.Append("F08='" + model.active + "',");
            strSql.Append("F19=" + model.casePack + ",");
            strSql.Append("F23='" + model.netWT + "',");
            strSql.Append("F24=" + model.wt + ",");
            strSql.Append("F27='" + model.vendor + "',");
            strSql.Append("F29='" + model.desc + "',");
            strSql.Append("F30=" + model.price + ",");
            strSql.Append("F31=" + model.multple + ",");
            strSql.Append("F38=" + model.caseCost + ",");
            strSql.Append("F79='" + model.foodStamp + "',");
            strSql.Append("F81='" + model.tax1 + "',");
            strSql.Append("F82='" + model.scaleWeight + "',");
            strSql.Append("F178='" + model.wic + "',");
            strSql.Append("F924='" + model.cvv + "',");
            strSql.Append("create_datetime='" + model.create_datetime + "',");
            strSql.Append("update_datetime='" + model.update_datetime + "',");
            strSql.Append("F106='" + model.followdep + "',");
            strSql.Append("F96='" + model.tax2 + "',");
            strSql.Append("F97='" + model.tax3 + "',");
            strSql.Append("F98='" + model.tax4 + "',");
            strSql.Append("F150='" + model.item_discount_flag + "',");
            strSql.Append("F158='" + model.bottle_deposit + "',");
            strSql.Append("is_loyalty='" + model.is_loyalty + "',");
            strSql.Append("is_tare=" + model.is_tare + ",");
            strSql.Append("is_age_ver=" + model.is_age_ver + ",");
            strSql.Append("F99='" + model.tax5 + "',");
            strSql.Append("F100='" + model.tax6 + "',");
            strSql.Append("is_coupon='" + model.is_coupon + "',");
            strSql.Append("service_fl='" + model.service_fl + "',");
            strSql.Append("printer_id=" + model.printer_id + ",");
            strSql.Append("is_mult_coupon='" + model.is_mult_coupon + "',");
            strSql.Append("is_price_lvl=" + model.is_price_lvl + "");
            strSql.Append(" where F01='" + model.upc + "' ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string F01)
        {
            DbHelper db = new DbHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_item_setting ");
            strSql.Append(" where F01='" + F01 + "' ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int rows = db.ExecuteNonQuery(dbCommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }		/// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string F01list)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_item_setting ");
            strSql.Append(" where F01 in (" + F01list + ")  ");
            DbHelper db = new DbHelper();
            DbCommand dbcommand = db.GetSqlStringCommand(strSql.ToString());
            int rows = db.ExecuteNonQuery(dbcommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MG_WIN.Model.ItemSetting GetModel(string F01)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select   ");
            strSql.Append(" F01,F02,F03,F04,F08,F19,F23,F24,F27,F29,F30,F31,F38,F79,F81,F82,F178,F924,create_datetime,update_datetime,F106,F96,F97,F98,F150,F158,is_loyalty,is_tare,is_age_ver,F99,F100,is_coupon,service_fl,printer_id,is_mult_coupon,is_price_lvl ");
            strSql.Append(" from t_item_setting ");
            strSql.Append(" where F01='" + F01 + "' ");
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            MG_WIN.Model.ItemSetting model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MG_WIN.Model.ItemSetting DataRowToModel(DataRow row)
        {
            MG_WIN.Model.ItemSetting model = new MG_WIN.Model.ItemSetting();
            if (row != null)
            {
                if (row["F01"] != null)
                {
                    model.upc = row["F01"].ToString();
                }
                if (row["F02"] != null)
                {
                    model.name = row["F02"].ToString();
                }
                if (row["F03"] != null && row["F03"].ToString() != "")
                {
                    model.dep = int.Parse(row["F03"].ToString());
                }
                if (row["F04"] != null && row["F04"].ToString() != "")
                {
                    model.subDep = int.Parse(row["F04"].ToString());
                }
                if (row["F08"] != null)
                {
                    model.active = row["F08"].ToString();
                }
                if (row["F19"] != null && row["F19"].ToString() != "")
                {
                    model.casePack = int.Parse(row["F19"].ToString());
                }
                if (row["F23"] != null)
                {
                    model.netWT = row["F23"].ToString();
                }
                if (row["F24"] != null && row["F24"].ToString() != "")
                {
                    model.wt = double.Parse(row["F24"].ToString());
                }
                if (row["F27"] != null)
                {
                    model.vendor = row["F27"].ToString();
                }
                if (row["F29"] != null)
                {
                    model.desc = row["F29"].ToString();
                }
                if (row["F30"] != null && row["F30"].ToString() != "")
                {
                    model.price = double.Parse(row["F30"].ToString());
                }
                if (row["F31"] != null && row["F31"].ToString() != "")
                {
                    model.multple = int.Parse(row["F31"].ToString());
                }
                if (row["F38"] != null && row["F38"].ToString() != "")
                {
                    model.caseCost = double.Parse(row["F38"].ToString());
                }
                if (row["F79"] != null)
                {
                    model.foodStamp = row["F79"].ToString();
                }
                if (row["F81"] != null)
                {
                    model.tax1 = row["F81"].ToString();
                }
                if (row["F82"] != null)
                {
                    model.scaleWeight = row["F82"].ToString();
                }
                if (row["F178"] != null)
                {
                    model.wic = row["F178"].ToString();
                }
                if (row["F924"] != null)
                {
                    model.cvv = row["F924"].ToString();
                }
                if (row["create_datetime"] != null)
                {
                    model.create_datetime = row["create_datetime"].ToString();
                }
                if (row["update_datetime"] != null)
                {
                    model.update_datetime = row["update_datetime"].ToString();
                }
                if (row["F106"] != null)
                {
                    model.followdep = row["F106"].ToString();
                }
                if (row["F96"] != null)
                {
                    model.tax2 = row["F96"].ToString();
                }
                if (row["F97"] != null)
                {
                    model.tax3 = row["F97"].ToString();
                }
                if (row["F98"] != null)
                {
                    model.tax4 = row["F98"].ToString();
                }
                if (row["F150"] != null)
                {
                    model.item_discount_flag = row["F150"].ToString();
                }
                if (row["F158"] != null)
                {
                    model.bottle_deposit = row["F158"].ToString();
                }
                if (row["is_loyalty"] != null)
                {
                    model.is_loyalty = row["is_loyalty"].ToString();
                }
                if (row["is_tare"] != null && row["is_tare"].ToString() != "")
                {
                    model.is_tare = int.Parse(row["is_tare"].ToString());
                }
                if (row["is_age_ver"] != null && row["is_age_ver"].ToString() != "")
                {
                    model.is_age_ver = row["is_age_ver"].ToString();
                }
                if (row["F99"] != null)
                {
                    model.tax5 = row["F99"].ToString();
                }
                if (row["F100"] != null)
                {
                    model.tax6 = row["F100"].ToString();
                }
                if (row["is_coupon"] != null)
                {
                    model.is_coupon = row["is_coupon"].ToString();
                }
                if (row["service_fl"] != null)
                {
                    model.service_fl = row["service_fl"].ToString();
                }
                if (row["printer_id"] != null && row["printer_id"].ToString() != "")
                {
                    model.printer_id = row["printer_id"].ToString();
                }
                if (row["is_mult_coupon"] != null)
                {
                    model.is_mult_coupon = row["is_mult_coupon"].ToString();
                }
                if (row["is_price_lvl"] != null && row["is_price_lvl"].ToString() != "")
                {
                    model.is_price_lvl = int.Parse(row["is_price_lvl"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F01,F02,F03,F04,F08,F19,F23,F24,F27,F29,F30,F31,F38,F79,F81,F82,F178,F924,create_datetime,update_datetime,F106,F96,F97,F98,F150,F158,is_loyalty,is_tare,is_age_ver,F99,F100,is_coupon,service_fl,printer_id,is_mult_coupon,is_price_lvl ");
            strSql.Append(" FROM t_item_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(cmd);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" F01,F02,F03,F04,F08,F19,F23,F24,F27,F29,F30,F31,F38,F79,F81,F82,F178,F924,create_datetime,update_datetime,F106,F96,F97,F98,F150,F158,is_loyalty,is_tare,is_age_ver,F99,F100,is_coupon,service_fl,printer_id,is_mult_coupon,is_price_lvl ");
            strSql.Append(" FROM t_item_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(cmd);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_item_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            object obj = db.ExecuteScalar(dbCommand);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from t_item_setting T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(cmd);
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "t_item_setting");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "System.Collections.Generic.List`1[Maticsoft.CodeHelper.ColumnInfo]");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<MG_WIN.Model.ItemSetting> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F01,F02,F03,F04,F08,F19,F23,F24,F27,F29,F30,F31,F38,F79,F81,F82,F178,F924,create_datetime,update_datetime,F106,F96,F97,F98,F150,F158,is_loyalty,is_tare,is_age_ver,F99,F100,is_coupon,service_fl,printer_id,is_mult_coupon,is_price_lvl ");
            strSql.Append(" FROM t_item_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<MG_WIN.Model.ItemSetting> list = new List<MG_WIN.Model.ItemSetting>();
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            using (IDataReader dataReader = db.ExecuteReader(cmd))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public MG_WIN.Model.ItemSetting ReaderBind(IDataReader dataReader)
        {
            MG_WIN.Model.ItemSetting model = new MG_WIN.Model.ItemSetting();
            
            model.upc = dataReader["F01"].ToString();
            model.name = dataReader["F02"].ToString();
            model.dep = Convert.ToInt32(dataReader["F03"]);
            model.subDep = Convert.ToInt32(dataReader["F04"]);
            model.active = dataReader["F08"].ToString();
            model.casePack = Convert.ToInt32(dataReader["F19"]);
            model.netWT = dataReader["F23"].ToString();
            model.wt = Convert.ToDouble(dataReader["F24"]);
            model.vendor = dataReader["F27"].ToString();
            model.desc = dataReader["F29"].ToString();
            model.price = Convert.ToDouble(dataReader["F30"]);
            model.multple = Convert.ToInt32(dataReader["F31"]);
            model.caseCost = Convert.ToDouble(dataReader["F38"]);
            model.foodStamp = dataReader["F79"].ToString();
            model.tax1 = dataReader["F81"].ToString();
            model.scaleWeight = dataReader["F82"].ToString();
            model.wic = dataReader["F178"].ToString();
            model.cvv = dataReader["F924"].ToString();
            model.create_datetime = dataReader["create_datetime"].ToString();
            model.update_datetime = dataReader["update_datetime"].ToString();
            model.followdep = dataReader["F106"].ToString();
            model.tax2 = dataReader["F96"].ToString();
            model.tax3 = dataReader["F97"].ToString();
            model.tax4 = dataReader["F98"].ToString();
            model.item_discount_flag = dataReader["F150"].ToString();
            model.bottle_deposit = dataReader["F158"].ToString();
            model.is_loyalty = dataReader["is_loyalty"].ToString();
            model.is_tare = Convert.ToInt32(dataReader["is_tare"]);
            model.is_age_ver = dataReader["is_age_ver"].ToString();
            model.tax5 = dataReader["F99"].ToString();
            model.tax6 = dataReader["F100"].ToString();
            model.is_coupon = dataReader["is_coupon"].ToString();
            model.service_fl = dataReader["service_fl"].ToString();
            model.printer_id = dataReader["printer_id"].ToString();
            model.is_mult_coupon = dataReader["is_mult_coupon"].ToString();
            model.is_price_lvl = Convert.ToInt32(dataReader["is_price_lvl"]);
            return model;
        }

        #endregion  Method
    }
}